package v1

import (
	"github.com/gin-gonic/gin"
	"jmicro/app/jshop/api/internal/controller"
	"jmicro/app/jshop/api/internal/domain/request"
	v12 "jmicro/app/jshop/api/internal/service/v1"
	"jmicro/app/pkg/code"
	gin2 "jmicro/app/pkg/translator/gin"
	"jmicro/pkg/common/core"
	"jmicro/pkg/errors"
	"jmicro/pkg/storage"
	"time"
)

func (us *userServer) SendSms(c *gin.Context) {
	sendSmsForm := request.SendSmsForm{}
	if err := c.ShouldBind(&sendSmsForm); err != nil {
		gin2.HandleValidatorError(c, err, controller.Trans)
	}
	smsCode := v12.GenerateSmsCode(6)
	err := us.smsSrv.SendSms(c, sendSmsForm.Mobile, "SMS_181850725", "{\"code\":"+smsCode+"}")
	if err != nil {
		core.WriteResponse(c, errors.WithCode(code.ErrSmsSend, err.Error()), nil)
		return
	}
	//将验证码保存起来
	rstore := storage.RedisCluster{}
	err = rstore.SetKey(c, sendSmsForm.Mobile, smsCode, 5*time.Minute)
	if err != nil {
		core.WriteResponse(c, errors.WithCode(code.ErrSmsSend, err.Error()), nil)
		return
	}
	core.WriteResponse(c, nil, nil)
}
